package com.hd.trans.framework.dialog;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.hd.trans.R;
/**
 * 介绍：自定义PopupWindow
 * 作者：qianjujun
 * 邮箱：qianjujun@163.com
 * 时间: 2017-03-30  15:59
 */

public class CstPup extends PopupWindow {

    protected View parent;
    private View mMaskView;
    protected Activity mContext;

    private OnDismissListener onDismissListener;

    @SuppressLint("WrongConstant")
    public CstPup(@Nullable View parentView, @NonNull View contentView, boolean isTouch, boolean isMatch) {
        setContentView(contentView);
        if (isTouch) {
            setOutsideTouchable(true);
            setFocusable(true);
        }
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        if (isMatch) {
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        } else {
            setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        }
        mContext = (Activity) contentView.getContext();
        if (parentView == null) {
            parent = mContext.findViewById(android.R.id.content);
        } else {
            parent = parentView;
        }
        setSoftInputMode(INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        superSetOnDismissListener(this::dispatchDismiss);
    }


    @SuppressLint("WrongConstant")
    public CstPup(View view, int index) {
        setContentView(view);
        setOutsideTouchable(true);
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mContext = (Activity) view.getContext();
        parent = mContext.findViewById(android.R.id.content);
        setSoftInputMode(INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        superSetOnDismissListener(this::dispatchDismiss);
    }

    @SuppressLint("WrongConstant")
    public CstPup(View parentView,View view) {
        this(parentView,view, true, true);
    }

    @SuppressLint("WrongConstant")
    public CstPup(View view) {
        this(null,view, true, true);
    }

    @SuppressLint("WrongConstant")
    public CstPup(View view, boolean isMatch) {
        this(null,view, true, isMatch);
    }

    public int getMeasuredHeight(){
        View contentView = getContentView();
        if(contentView == null)
            return 0;
        int height = contentView.getMeasuredHeight();
        if (height == 0){
            contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            height = contentView.getMeasuredHeight();
        }
        return height;
    }

    public int getMeasuredWidth(){
        View contentView = getContentView();
        if(contentView == null)
            return 0;
        int width = contentView.getMeasuredWidth();
        if (width == 0){
            contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
            width = contentView.getMeasuredWidth();
        }
        return width;
    }

    protected float getMDimAmount(){
        return 0.6f;
    }

    @Override
    public void setOnDismissListener(OnDismissListener listener) {
        this.onDismissListener = listener;
    }

    private void superSetOnDismissListener(OnDismissListener listener){
        super.setOnDismissListener(listener);
    }


    public void showFromBottom() {
        maskViewShow(true);
        setAnimationStyle(R.style.BottomAnimation);
        showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

    private Integer systemUiVisibilityCache = null;

    @Nullable
    private View getMaskView(){
        if (mMaskView != null)
            return mMaskView;
        if (parent == null)
            return null;
        if (!(parent instanceof ViewGroup))
            return null;

        mMaskView = parent.findViewById(R.id.cstpup_mask);
        if(mMaskView == null){
            mMaskView = new View(parent.getContext());
            mMaskView.setId(R.id.cstpup_mask);
            mMaskView.setBackgroundColor(Color.BLACK);
            mMaskView.setVisibility(View.GONE);
            ((ViewGroup)parent).addView(mMaskView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }

        return mMaskView;
    }

    protected void maskViewShow(boolean show) {
        float dimAmount = getMDimAmount();
        if (dimAmount <= 0f)
            return;

        View maskView = getMaskView();
        if (maskView == null)
            return;

        if (show){
            maskView.setAlpha(0f);
            maskView.setVisibility(View.VISIBLE);
            ObjectAnimator.ofFloat(maskView, View.ALPHA.getName(), 0f,dimAmount).setDuration(150).start();

            View decorView = mContext.getWindow().getDecorView();
            int visibility = decorView.getSystemUiVisibility();
            int visibilityNew = visibility & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            if (visibilityNew != visibility){
                if (systemUiVisibilityCache == null) {
                    systemUiVisibilityCache = visibility;
                }
                decorView.setSystemUiVisibility(visibilityNew);
            }
        }
        else {
            maskView.setVisibility(View.GONE);
            restoreSystemUiVisibility();
        }
    }

    private void restoreSystemUiVisibility(){
        if (systemUiVisibilityCache != null){
            mContext.getWindow().getDecorView().setSystemUiVisibility(systemUiVisibilityCache);
            systemUiVisibilityCache = null;
        }
    }

    public void showCenter() {
        maskViewShow(true);
        setAnimationStyle(R.style.CenterDialogStyle);
        showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        onShowed();
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff, int gravity) {
        super.showAsDropDown(anchor, xoff, yoff, gravity);
        onShowed();
    }

    private void dispatchDismiss() {
        onDismiss();
        maskViewShow(false);
        if (onDismissListener != null) {
            onDismissListener.onDismiss();
        }
    }

    protected void onShowed(){

    }

    protected void onDismiss() {

    }

}
